package com.delianna.bwt.data;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.delianna.bwt.models.BWT;
import com.delianna.bwt.models.Lane;
import com.delianna.bwt.models.Lanes;
import com.delianna.bwt.models.Port;

/**
 * @author Delio Castillo (jangeador@gmail.com)
 *
 */
public class DataHandler extends DefaultHandler {
	private String _position;
	private Port currentPort;
	private Lanes currentLanes;
	private Lane currentLane;
	private String laneClass;

	// this holds the data
	private BWT _bwt;

	/**
	 * Returns the data object
	 * 
	 * @return
	 */
	public BWT getData() {
		return _bwt;
	}

	/**
	 * This gets called when the xml document is first opened
	 * 
	 * @throws SAXException
	 */
	@Override
	public void startDocument() throws SAXException {
		_bwt = new BWT();
	}

	/**
	 * Called when it's finished handling the document
	 * 
	 * @throws SAXException
	 */
	@Override
	public void endDocument() throws SAXException {
	}

	/**
	 * This gets called at the start of an element. Here we're also setting the
	 * booleans to true if it's at that specific tag. (so we know where we are)
	 * 
	 * @param namespaceURI
	 * @param localName
	 * @param qName
	 * @param atts
	 * @throws SAXException
	 */
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		_position = localName;

		// PORT
		if (_position.equals("port")) {
			currentPort = new Port();
		}
		
		// LANES
		if (_position.equals("commercial_vehicle_lanes")) {
			laneClass = "Commercial";
			currentLanes = new Lanes();
		} else if (_position.equals("passenger_vehicle_lanes")) {
			laneClass = "Passenger";
			currentLanes = new Lanes();
		} else if (_position.equals("pedestrian_lanes")) {
			laneClass = "Pedestrian";
			currentLanes = new Lanes();
		}

		// LANE
		if (_position.equals("standard_lanes")) {
			currentLane = new Lane();
			currentLane.setLaneClass(laneClass);
			currentLane.setLaneType(_position);
		} else if (_position.equals("FAST_lanes")) {
			currentLane = new Lane();
			currentLane.setLaneClass(laneClass);
			currentLane.setLaneType(_position);
		} else if (_position.equals("NEXUS_SENTRI_lanes")) {
			currentLane = new Lane();
			currentLane.setLaneClass(laneClass);
			currentLane.setLaneType(_position);
		}

	}

	/**
	 * Called at the end of the element. Setting the booleans to false, so we
	 * know that we've just left that tag.
	 * 
	 * @param namespaceURI
	 * @param localName
	 * @param qName
	 * @throws SAXException
	 */
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		//PORT
		if (localName.equals("port")) {
			_bwt.addPort(currentPort);
		}

		// LANES
		else if (localName.equals("commercial_vehicle_lanes")) {
			currentPort.setCommercialLanes(currentLanes);
		} else if (localName.equals("passenger_vehicle_lanes")) {
			currentPort.setPassengerLanes(currentLanes);
		} else if (localName.equals("pedestrian_lanes")) {
			currentPort.setPedestrianLanes(currentLanes);
		}

		// LANE
		if (localName.equals("standard_lanes")) {
			currentLanes.setStandardLane(currentLane);
		} else if (localName.equals("FAST_lanes")) {
			currentLanes.setExpressLane(currentLane);
		} else if (localName.equals("NEXUS_SENTRI_lanes")) {
			currentLanes.setExpressLane(currentLane);
		}

	}

	/**
	 * Calling when we're within an element. Here we're checking to see if there
	 * is any content in the tags that we're interested in and populating it in
	 * the Config object.
	 * 
	 * @param ch
	 * @param start
	 * @param length
	 */
	@Override
	public void characters(char ch[], int start, int length) {
		String chars = new String(ch, start, length);
		chars = chars.trim();

		// THIS IS BWT PROCESSING
		if (_position.equals("last_updated_date")) {
			_bwt.last_updated_date = chars;
		} else if (_position.equals("last_updated_time")) {
			_bwt.last_updated_time = chars;
		} else if (_position.equals("number_of_ports")) {
			_bwt.number_of_ports = chars;
		}
		// PORT PROCESSING
		else if (_position.equals("port_number")) {
			currentPort.port_number = chars;
		} else if (_position.equals("border")) {
			currentPort.border = chars;
		} else if (_position.equals("port_name")) {
			currentPort.port_name = chars;
		} else if (_position.equals("crossing_name")) {
			currentPort.crossing_name = chars;
		} else if (_position.equals("hours")) {
			currentPort.hours = chars;
		} else if (_position.equals("date")) {
			currentPort.setDate(chars);
		} else if (_position.equals("port_status")) {
			currentPort.port_status = chars;
		}
		// LANES PROCESSING
		else if (_position.equals("maximum_lanes")) {
			currentLanes.maximum_lanes = chars;
		}

		// LANE PROCESSING
		else if (_position.equals("operational_status")) {
			currentLane.setOperationalStatus(chars);
		} else if (_position.equals("update_time")) {
			currentLane.setUpdateTime(chars);
		} else if (_position.equals("delay_minutes")) {
			currentLane.setDelayMinutes(chars);
		} else if (_position.equals("lanes_open")) {
			currentLane.setLanesOpen(chars);
		}
	}
}
